from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from app.schemas.base import BaseSchema, PageInfo, ResponseBase, PageResponseBase

class RequestLogBase(BaseModel):
    """请求日志基础模式"""
    user_id: Optional[int] = Field(None, description="用户ID")
    username: Optional[str] = Field(None, description="用户名")
    ip_address: str = Field(..., description="IP地址")
    method: str = Field(..., description="请求方法")
    path: str = Field(..., description="请求路径")
    status_code: int = Field(..., description="状态码")
    response_time: float = Field(..., description="响应时间(ms)")
    user_agent: Optional[str] = Field(None, description="用户代理")

class RequestLogOut(RequestLogBase, BaseSchema):
    """请求日志输出模式"""
    request_body: Optional[str] = Field(None, description="请求体")
    response_body: Optional[str] = Field(None, description="响应体")

class RequestLogQuery(BaseModel):
    """请求日志查询模式"""
    username: Optional[str] = Field(None, description="用户名")
    ip_address: Optional[str] = Field(None, description="IP地址")
    method: Optional[str] = Field(None, description="请求方法")
    url: Optional[str] = Field(None, description="请求路径")  # 修改为url以匹配前端参数
    status_code: Optional[int] = Field(None, description="状态码")
    start_time: Optional[str] = Field(None, description="开始时间")  # 改为字符串类型，更灵活处理
    end_time: Optional[str] = Field(None, description="结束时间")  # 改为字符串类型，更灵活处理
    page: int = Field(1, description="页码")
    page_size: int = Field(10, description="每页条数")

class LoginHistoryBase(BaseModel):
    """登录历史基础模式"""
    user_id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    ip_address: str = Field(..., description="IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    login_status: bool = Field(..., description="登录状态：True-成功，False-失败")
    login_message: Optional[str] = Field(None, description="登录消息")

class LoginHistoryOut(LoginHistoryBase, BaseSchema):
    """登录历史输出模式"""
    pass

class LoginHistoryQuery(BaseModel):
    """登录历史查询模式"""
    username: Optional[str] = Field(None, description="用户名")
    ip: Optional[str] = Field(None, description="IP地址")  # 修改为ip以匹配前端参数
    status: Optional[str] = Field(None, description="登录状态")  # 改为字符串类型，手动解析
    start_time: Optional[str] = Field(None, description="开始时间")  # 改为字符串类型，更灵活处理
    end_time: Optional[str] = Field(None, description="结束时间")  # 改为字符串类型，更灵活处理
    page: int = Field(1, description="页码")
    page_size: int = Field(10, description="每页条数")
